﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.MediaCenter.UI;
using System.Diagnostics;
using MyVideoViewerLibrary;

namespace MyVideoViewerMCPlugin
{
    public class SeasonGallery : GalleryPage
    {
        private Season currentSeason;
        public Season CurrentSeason
        {
            get { return currentSeason; }
            set
            {
                if (currentSeason != value)
                {
                    currentSeason = value;
                    FirePropertyChanged("CurrentSeason");
                }
            }
        }
        public Image BackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(CurrentSeries.FolderLocation, "fanart.jpg");
                if (File.Exists(imageLocation))
                    return new Image("file://" + imageLocation);
                else
                    return  new Image("resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/clear");
            }
        }
        public bool ShowBackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(CurrentSeries.FolderLocation, "fanart.jpg");
                return File.Exists(imageLocation);
            }
        }

        public Series CurrentSeries { get; set; }
        public List<Season> Seasons { get; set; }

        public SeasonGallery()
        { }

        public SeasonGallery(Series series)
        {
            ItemSize = GalleryItemSize.Large;
            Description = series.SeriesName;

            

            CurrentSeries = series;
            Seasons = CurrentSeries.Seasons.Where(s => s.Episodes.Where(e => !string.IsNullOrEmpty(e.FileLocation)).Count() > 0).OrderBy(s => s.SeasonNumber).ToList();

            VirtualList galleryList = new VirtualList(this, null);
            galleryList.EnableSlowDataRequests = true;
            galleryList.RequestSlowDataHandler = new RequestSlowDataHandler(CompleteGalleryItem);
            Content = galleryList;

            CreateGalleryFilters();
        }

        private void CreateGalleryFilters()
        {
            ArrayListDataSet list = new ArrayListDataSet(this);

            // Create the unfiltered "All" filter
            ModelItem filter = new Filter(this, Resources.GroupBy_Title, 0);

            foreach (Season s in Seasons)
                this.Content.Add(CreateGalleryItem(s, "", false));
            if (CurrentSeries.Series_Persons.Count > 0)
            {
                Season actors = new Season() { SeasonNumber = -1 };
                this.Content.Add(CreateGalleryItem(actors, "", false));
            }
        }

        private ThumbnailCommand CreateGalleryItem(Season seasonData, string description, bool isHeading)
        {
            ThumbnailCommand item = new ThumbnailCommand(this);
            item.Description = seasonData.Name;
            item.Data = seasonData;
            item.ShowHeading = isHeading;
            //item.ItemId = (int)movieData["Movie_ID"];

            // Display the release year as this item's metadata.
            //DateTime releaseDate = movieData.ReleaseDate;
            item.Metadata = description;

            item.Invoked += delegate(object sender, EventArgs args)
            {
                ThumbnailCommand galleryItem = (ThumbnailCommand)sender;
                Season season = (Season)galleryItem.Data;
                if(season.SeasonNumber != -1)
                    Application.Current.GoToGalleryPage(new EpisodeGallery(season));
                else
                    Application.Current.GoToGalleryPage(new ActorsGallery(CurrentSeries));
            };

            item.OnFocus += delegate(object sender, EventArgs args)
            {
                ThumbnailCommand galleryItem = (ThumbnailCommand)sender;

                SeasonGallery page = (SeasonGallery)galleryItem.Owner;
                page.CurrentSeason = (Season)galleryItem.Data;
            };

            return item;
        }

        public override int JumpTo(string searchText)
        {
            searchText = searchText.ToLower();
            int seasonNumber;
            if (!int.TryParse(searchText, out seasonNumber))
                return -1;
            Season season = Seasons.SingleOrDefault(s => s.SeasonNumber == seasonNumber);
            if (season == null)
                return -1;
            else
            {
                int i = Seasons.IndexOf(season);
                return i;
            }
        }

        #region slowdatahandlers
        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(VirtualList list, int index)
        {
            ThumbnailCommand item = (ThumbnailCommand)list[index];
            CompleteGalleryItem(item, (Season)item.Data);
        }

        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(ThumbnailCommand item, Season seasonData)
        {
            string imageName = "file://" + seasonData.ImageLocation;
            if(seasonData.SeasonNumber == -1)
                imageName = "resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/actors";
            item.Image = LoadImage(imageName);
        }
        #endregion
    }
}
